package com.kefu.admin.modules.tgqmdj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kefu.admin.modules.sys.vo.base.PageResultVo;
import com.kefu.admin.modules.tgqmdj.model.Tgctms;
import com.kefu.admin.modules.tgqmdj.service.TgdService;
import com.kefu.admin.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by TWD on 2019/1/28.
 */
@Controller
@RequestMapping("/mobile")
public class TgqmdjMobileController {

    private static final Logger logger = LoggerFactory.getLogger(TgqmdjMobileController.class);

    private Map<String,String> mapToken = new HashMap<>();

    @Autowired
    private TgdService tgdService;

    /*首页*/
    @RequestMapping(value={"/index"})
    public ModelAndView index(HttpServletRequest request, ModelMap mmap){
        ModelAndView  mav = new ModelAndView("guanyuanmobile/index") ;
        mav.addObject("tgdqmdjtitle","") ;
        return  mav;
    }


    /*首页*/
    @RequestMapping(value={"/gysz"})
    public ModelAndView gysz(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/gys") ;
        return  mav;
    }

    /*首页*/
    @RequestMapping(value={"/ct"})
    public ModelAndView ct(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/hall") ;
        return  mav;
    }



    /*首页*/
    @RequestMapping(value={"/twelve"})
    public ModelAndView twelve(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/twelve") ;
        return  mav;
    }

    @RequestMapping(value={"/tgy"})
    public ModelAndView tgy(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/ygy") ;
        return  mav;
    }

    @RequestMapping(value={"/dz"})
    public ModelAndView dz(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/dz") ;
        return  mav;
    }
    /*首页*/
    @RequestMapping(value={"/article"})
    public ModelAndView article(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/article") ;
        return  mav;
    }


    @RequestMapping(value={"/dzinfo"})
    @ResponseBody
    public R dzinfo(HttpServletRequest request, ModelMap mmap){
        String url = "http://mp.weixin.qq.com/mp/homepage?__biz=MzU1MjkzNDAxOQ==&hid=6&sn=ca1fd07eaa411cb496f92d5a6800493f&scene=18";

        Map<String, String> paramMap = new HashMap<String, String>();

        try {
            Result result= SendRequest.sendPost(url, null, paramMap, "utf-8");
            String str = result.getHtml(result, "utf-8");
            System.out.println(str);
            return R.ok().put("data",str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok();
    }



    /*留言*/
    @RequestMapping(value={"/infinite"})
    public ModelAndView infinite(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/infinite") ;
        return  mav;
    }

    /*福禄i*/
    @RequestMapping(value={"/product"})
    public ModelAndView product(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/product") ;
        return  mav;
    }


    /*福禄i*/
    @RequestMapping(value={"/szsp"})
    public ModelAndView szsp(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/panel") ;
        return  mav;
    }

    /*福禄i*/
    @RequestMapping(value={"/videopanel"})
    public ModelAndView videopanel(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/videopanel") ;
        return  mav;
    }

    @RequestMapping(value={"/dl"})
    public ModelAndView dl(HttpServletRequest request, ModelMap mmap){
        ModelAndView mav = new ModelAndView("guanyuanmobile/dl") ;
        return  mav;
    }

    @RequestMapping(value={"/ctsubmit"})
    @ResponseBody
    public R ctsubmit(HttpServletRequest request, ModelMap mmap, Tgctms tgctms){
        try {
            String token = mapToken.get(request.getSession().getId());
            if(null!=token && !"".equals(token)){
                tgdService.saveCtMsg(tgctms);
                return  R.ok();
            }

        }catch (Exception ex){
            logger.error("提交留言失败：{}",ex);
        }
        return  R.error("失败");
    }


    @RequestMapping(value={"/pwdv"})
    @ResponseBody
    public R pwdv(HttpServletRequest request, ModelMap mmap){
        try {
            String pwd = request.getParameter("pwd");
           if("4554".equals(pwd)){
               mapToken.put(request.getSession().getId(),TokenGenerator.generateValue());
               return  R.ok();
           }
        }catch (Exception ex){
            logger.error("提交留言失败：{}",ex);
        }
        return  R.error("失败");
    }

    @RequestMapping("/loadctdata")
    @ResponseBody
    public R loadWenzhang(Tgctms tgctms, Integer limit, Integer offset){
        PageHelper.startPage(offset,limit);
        List<Tgctms> tgctmsList = tgdService.queryCtMsg(tgctms);
        PageInfo<Tgctms> pages = new PageInfo<>(tgctmsList);
        return R.ok().put("data",pages);
    }


}
